package gui;

import Presenter.Presenter;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import model.RemoteUser;

/**
 *
 * @author julien
 */
//implements FromUser, ToUser
public class Window extends JFrame {
    private Presenter presenter;
    
    // Jpanel écran de connection
    private JPanel connection_panel;
    private JLabel welcome_message;
    private JTextField nickname_field;
    private JButton connection_button;
    
    //Jpanel avec tous les éléments de chat
    private JPanel chat_panel;
    DefaultListModel<RemoteUser> remote_users_list;
    JList remote_users_jlist;
    JTextPane messages_pane;
    
    private JPanel local_user_panel;
    private JLabel local_user;
    private JButton deconnection_button;
    
    private JPanel message_send_panel;
    private JTextArea message_area;
    private JButton send_button;
    
    private JButton sendFile_button;
    private JFileChooser fc;
    
            
   public Window(Presenter presenter) {
       this.presenter = presenter;
       initComponents();
   }

    private void initComponents() {
        
        //Configuration de la fenêtre
        this.setTitle("ChatSystem");
        //this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        
        this.remote_users_list = new <RemoteUser>DefaultListModel();
        this.remote_users_jlist = new JList(this.remote_users_list);
        this.remote_users_jlist.setCellRenderer(new CellRenderer());
        this.remote_users_jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.remote_users_jlist.setBackground(new Color(44, 62, 80));
        this.remote_users_jlist.setOpaque(true);

        
        
        this.connection_panel = new JPanel(new GridBagLayout());
        GridBagConstraints c_connection_panel = new GridBagConstraints();
        c_connection_panel.insets = new Insets(20, 20, 5, 20); //insets définir la marge entre les composant
        //c_connection_panel.fill = GridBagConstraints.BOTH;
        
        this.welcome_message = new JLabel("Welcome !");
        this.nickname_field = new JTextField(30);
        
        this.connection_button = new JButton("Connect");
        this.connection_button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //On met le JPanel spécifique à la connection
                //On supprime tous les élements des listes le Jlabel...
                if(!("").equals(Window.this.nickname_field.getText())){
                    Window.this.local_user.setText(Window.this.nickname_field.getText());
                    Window.this.getContentPane().remove(Window.this.connection_panel);
                    Window.this.getContentPane().add(Window.this.chat_panel);
                    Window.this.pack();
                    Window.this.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    Window.this.presenter.processConnection(Window.this.nickname_field.getText());
                    Window.this.nickname_field.setText("");
                    
//pack
                    //setvisible
                }
            }
        });
        
        
        c_connection_panel.gridx = 0; //selection de la case ou mettre l'élément
        c_connection_panel.gridy = 0; //selection de la case ou mettre l'élément
        c_connection_panel.weightx = 0;
        c_connection_panel.weighty = 0;
        c_connection_panel.gridheight = 1;
        this.connection_panel.add(this.welcome_message, c_connection_panel);
       
        
        
        c_connection_panel.gridx = 0; //selection de la case ou mettre l'élément
        c_connection_panel.gridy = 1; //selection de la case ou mettre l'élément
        c_connection_panel.weightx = 0;
        c_connection_panel.weighty = 0;
        c_connection_panel.gridheight = 1;
        this.connection_panel.add(this.nickname_field, c_connection_panel);
        
        
        c_connection_panel.gridx = 0; //selection de la case ou mettre l'élément
        c_connection_panel.gridy = 2; //selection de la case ou mettre l'élément
        c_connection_panel.weightx = 0;
        c_connection_panel.weighty = 0;
        c_connection_panel.gridheight = 1;
        this.connection_panel.add(this.connection_button, c_connection_panel);
        
        
        this.add(this.connection_panel);
        
        
        
        //configuration du panel principal
        this.chat_panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(0, 0, 0, 0); //insets définir la marge entre les composant
        c.fill = GridBagConstraints.BOTH;
        
        
        
        this.local_user = new JLabel("Anonymous");
        this.local_user.setOpaque(true);
        this.deconnection_button = new JButton("Déconnection");
        //perform deconnection
        this.deconnection_button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //On met le JPanel spécifique à la connection
                //On supprime tous les élements des listes le Jlabel...
                Window.this.getContentPane().remove(Window.this.chat_panel);
                Window.this.messages_pane.setText("");
                Window.this.remote_users_list.clear();
                Window.this.getContentPane().add(Window.this.connection_panel);
                Window.this.pack();
                Window.this.presenter.processDeconnection();
            }
        });
        
       
        
        //definition de local_user_panel
        this.local_user_panel = new JPanel();
        this.local_user_panel.setLayout(new GridBagLayout());
        GridBagConstraints c_local_user_panel = new GridBagConstraints();
        
        c_local_user_panel.insets = new Insets(0, 0, 0, 0);
        c_local_user_panel.fill = GridBagConstraints.BOTH;
        //this.message_send_panel.setBackground(Color.red);
        
        c_local_user_panel.gridx = 0; //selection de la case ou mettre l'élément
        c_local_user_panel.gridy = 0; //selection de la case ou mettre l'élément
        c_local_user_panel.weightx = 1;
        c_local_user_panel.weighty = 1;
        c_local_user_panel.gridheight = 1;
        c_local_user_panel.gridwidth = 1;
        this.local_user_panel.add(this.deconnection_button, c_local_user_panel);
        
        
        
        
        
        this.message_area = new JTextArea(3,30);
        //this.message_area.setPreferredSize(new Dimension(50, 50));
        this.send_button = new JButton("Send");
        //perform send
        this.send_button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                
                System.out.println(Window.this.remote_users_jlist.getSelectedValue().getClass());
                
                if(!Window.this.remote_users_jlist.isSelectionEmpty()){
                    
                    if(!("").equals(Window.this.message_area.getText())){
                        //on affiche le message dans messages_list
                        String to_display = "To [" + Window.this.remote_users_jlist.getSelectedValue().toString() + "]: " + Window.this.message_area.getText();
                        if(("").equals(Window.this.messages_pane.getText())){
                            Window.this.messages_pane.setText(to_display);
                        }else{
                            Window.this.messages_pane.setText(Window.this.messages_pane.getText() + "\n" + to_display);
                        }
                        Window.this.presenter.processMessageToSend(Window.this.message_area.getText(), (RemoteUser) Window.this.remote_users_jlist.getSelectedValue());
                        Window.this.message_area.setText("");
                    }                    
                }    
            }
        });
        
        this.sendFile_button = new JButton("Send file");
        fc = new JFileChooser();
        //perform send file
        this.sendFile_button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fc.showOpenDialog(Window.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would open the file.
                    System.out.println("Opening: " + file.getName() + ". \n");

                    if(!Window.this.remote_users_jlist.isSelectionEmpty()){
                        Window.this.presenter.processEnvoieFichier(file, (RemoteUser) Window.this.remote_users_jlist.getSelectedValue());
                    }  
                } 
                else {
                    System.out.println("Open command cancelled by user. \n");
                } 
            }    
        });
        
        //Message_send_panel definition
        this.message_send_panel = new JPanel();
        this.message_send_panel.setLayout(new GridBagLayout());
        GridBagConstraints c_message_send_panel = new GridBagConstraints();
        
        
        
        c_message_send_panel.insets = new Insets(0, 0, 0, 0);
        c_message_send_panel.fill = GridBagConstraints.BOTH;
        //this.message_send_panel.setBackground(Color.red);
        
        c_message_send_panel.gridx = 0; //selection de la case ou mettre l'élément
        c_message_send_panel.gridy = 0; //selection de la case ou mettre l'élément
        c_message_send_panel.weightx = 1;
        c_message_send_panel.weighty = 1;
        c_message_send_panel.gridheight = 1;
        c_message_send_panel.gridwidth = 1;
        this.message_send_panel.add(this.message_area, c_message_send_panel);
        
        c_message_send_panel.gridx = 1; //selection de la case ou mettre l'élément
        c_message_send_panel.gridy = 0; //selection de la case ou mettre l'élément
        c_message_send_panel.weightx = 0;
        c_message_send_panel.weighty = 1;
        c_message_send_panel.gridheight = 1;
        this.message_send_panel.add(this.send_button, c_message_send_panel);
        
        
        c_message_send_panel.gridx = 2; //selection de la case ou mettre l'élément
        c_message_send_panel.gridy = 0; //selection de la case ou mettre l'élément
        c_message_send_panel.weightx = 0;
        c_message_send_panel.weighty = 1;
        c_message_send_panel.gridheight = 1;
        this.message_send_panel.add(this.sendFile_button, c_message_send_panel);
        
        
        
        this.messages_pane = new JTextPane();
        this.messages_pane.setEditable(false);
        this.messages_pane.setBackground(new Color(236, 240, 241));
        this.messages_pane.setOpaque(true);
     
        JScrollPane scroll_pane_messages_list = new JScrollPane();
        scroll_pane_messages_list.setViewportView(this.messages_pane);
        
        // GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady)
        
        c.gridx = 0; //selection de la case ou mettre l'élément
        c.gridy = 2; //selection de la case ou mettre l'élément
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.gridheight = 1;
        this.chat_panel.add(this.local_user_panel, c);
       
        
        
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.1;
        c.weighty = 1;
        c.gridheight = 2;
        this.chat_panel.add(this.remote_users_jlist, c);
         
        
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridheight = 2;
        this.chat_panel.add(scroll_pane_messages_list, c);
        
        
        
        c.gridx = 1;
        c.gridy = 2;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.gridheight = 1;
        this.chat_panel.add(this.message_send_panel, c);
        
        
        
        //this.add(chat_panel);
        //fermeture de la fenêtre
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            };
        });
        this.setVisible(true);
        this.pack();
    }
}
